import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import API from '../../apis';
import _ from 'lodash';
import window from '../../util';
import { message, Modal } from 'antd';
import validatorjs from 'validatorjs';
import { WeaLocaleProvider } from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

export interface ShareStoreProps {
    conditions: any;
    form: any;
    update: Function;
    clearError: Function;
    errors: any;
    loading?: boolean;
    onOk: (p, s) => void;
    onCancel: (e) => void;
    visible: boolean;
    singleVisible: boolean;
    billid: string;
    refObj: any;
    closeSingle: Function;
}

export default class ShareStore implements ShareStoreProps {
    @observable conditions = [];
    @observable form = new WeaForm();
    @observable visible = false;
    @observable singleVisible = false;
    @observable billid = '';
    @observable refObj = {};
    @observable errors = {} as any;
    @observable loading = false;
    @observable treeKey : string[];
    @observable imgSelectValue ="";
    @observable verticalValue = '';
    getRule = () => {
        let rule = {} as any;
        let errors = {} as any;
        let sharetype = this.form.$('sharetype').values();
        if (sharetype != '5') {
            rule[`relatedid${sharetype}`] = 'required';
            errors[`required.relatedid${sharetype}`] = getLabel(128937, "请选择") + getLabel(19117, "共享对象");
            if (sharetype == '4') {
                let isRoleLimited = this.form.$('isRoleLimited').values();
                if (isRoleLimited == '1') {
                    let rolefieldtype = this.form.$('rolefieldtype').values();
                    rule.rolefieldtype = 'required';
                    rule[`rolefield${rolefieldtype}`] = 'required';
                    errors['required.rolefieldtype'] = getLabel(128937, "383747");
                    errors[`required.rolefield${rolefieldtype}`] = getLabel(128937, "请选择") + getLabel(82386, "限制字段");
                }
            } else if (sharetype == '6') {
                let joblevel = this.form.$('joblevel').values();
                if (joblevel == '0') {
                    rule.jobleveltext1 = 'required';
                    errors['required.jobleveltext1'] = getLabel(125231, '请选择部门');
                } else if (joblevel == '1') {
                    rule.jobleveltext2 = 'required';
                    errors['required.jobleveltext2'] = getLabel(82181, '请选择分部');
                }
            }
        }
        return {
            rule,
            errors
        }
    }
    onCancel = () => {
        this.form.resetConditionValue();
        this.visible = false;
    }
    @action
    onOk = (params, success) => {
        const { rule, errors } = this.getRule();
        let value = '';
        let sharetype = this.form.$('sharetype').values();
        if (sharetype != '5') {
            let relatedid = 'relatedid' + sharetype;
            value = this.form.getFormDatas()[relatedid].value;
        }
        if (sharetype != '1' && !(this.form.$('showlevel').value[0] + "")) {
            this.refObj["showlevel"].showError();
            return false;
        }
        const validator = new validatorjs(this.form.getFormDatas(), rule, errors);
        if (value == '' && sharetype != '5') {
            let errors = validator.errors.errors;
            let _errors = {};
            for (let field in errors) {
                _errors[field] = errors[field].join();
            }
            this.update({
                errors: _errors
            })
            message.error(getLabel(30933, "必填信息不完整!"));
            this.loading = false;
            return;
        }
        this.loading = true;
        API.saveBatchShare({
            ...params,
            billids: this.treeKey ? _.uniq(this.treeKey.map(d => d.split('_')[0]))+"" 
               :this.imgSelectValue ? 
                 this.imgSelectValue
                    :this.verticalValue ?
                    this.verticalValue: window._xtable_CheckedCheckboxId(),
            ...this.form.values()
        }).then(({ status, error, tips }) => {
            if (status == '1') {
                // message.success(tips);
                Modal.info({
                    title: getLabel(386786, '信息确认'),
                    content: tips,
                    onOk() {
                    },
                })
                this.onCancel();
            } else {
                message.error(error);
                throw error;
            }
            this.loading = false;
            success && success();
        })

    }

    @action
    clearError = (name) => {
        if (name == 'sharetype') {
            this.errors = {};
        }
        this.errors[name] = '';
    }

    @action
    getBatchShareCondition = (params, callback?: Function) => {
        API.batchShareCondition(params).then(({ status, error, conditions }) => {
            if (status == '1') {
                this.conditions = conditions;
                this.form.initFormFieldObj(_.values(conditions))
                callback && callback();
            } else {
                message.error(error)
                throw error;
            }
        })
    }

    @action
    batchShare = (Search) => {
        if (!Search.checkSeleted()) {
            return;
        }
        if(Search.treeKey){
            this.treeKey = Search.treeKey;
        }
        if(Search){
            this.imgSelectValue = Search.imgSelectValue;
            this.verticalValue = Search.verticalListStore.selectValue;
        }
        const showBatchShare = () => {
            this.visible = true;
        }

        this.form.resetConditionValue();
        this.getBatchShareCondition({
            modeId: Search.modeId,
            formId: Search.formId
        }, showBatchShare);
    }
    closeSingle = () => {
        this.update({ singleVisible: false });
    }
    @action
    update = (param) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }
}
